package com.luhuiguo.gwt.mvp.dispatcher.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.luhuiguo.gwt.mvp.dispatcher.shared.exception.SecureException;
import com.luhuiguo.gwt.mvp.dispatcher.shared.rpc.Action;
import com.luhuiguo.gwt.mvp.dispatcher.shared.rpc.Result;

/**
 * 
 * @author LuHuiguo
 * @version $Id: SecuredDispatcherAsync.java 11 2009-12-26 13:39:17Z luhuiguo $
 */
public class SecuredDispatcherAsync extends AbstractDispatcherAsync {

    private static final SecuredDispatcherServiceAsync realService = GWT.create(SecuredDispatcherService.class);

    public SecuredDispatcherAsync(ExceptionHandler exceptionHandler) {
        super(exceptionHandler);
    }

    public <A extends Action<R>, R extends Result> void execute(final A action, final AsyncCallback<R> callback) {
        realService.execute(action, new AsyncCallback<Result>() {
            public void onFailure(Throwable caught) {
                SecuredDispatcherAsync.this.onFailure(action, caught, callback);
            }

            @SuppressWarnings({ "unchecked" })
            public void onSuccess(Result result) {
                SecuredDispatcherAsync.this.onSuccess(action, (R) result, callback);
            }
        });
    }

    protected <A extends Action<R>, R extends Result> void onFailure(A action, Throwable caught,
            final AsyncCallback<R> callback) {
        if (caught instanceof SecureException) {

        }

        super.onFailure(action, caught, callback);

    }

}